const express = require("express")
const router = express.Router()
const { getList, getDetail, newBlog, delBlog, updateBlog } = require("../contorl/blog")

const check = require("../middleware/check")

// 获取list
router.get("/list", check, (req,res,next)=>{
  const author = req.session.username || ''
  getList(author).then(result=>{
    res.json(
      result
    )
  })
})

router.get("/detail", check, (req,res,next) => {
  const id = req.query.id
  getDetail(id).then(result => {
    res.json(
      result
    )
  })
})

router.post("/new", check, (req,res,next) => {
  // body 中需包含用户
  const body = req.body
  const author = req.session.username
  newBlog(body,author).then(result => {
    res.json(result)
  })
})

router.post("/del", check, (req,res,next) => {
  const id = req.query.id
  delBlog(id).then(result => {
    res.json(result)
  })
})

router.post("/update", check, (req,res,next) => {
  const body = req.body
  updateBlog(body).then(result => {
    res.json(result)
  })
})

module.exports = router